<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String productCode = request.getParameter("productCode");
%>
 
<div class="easyui-layout" style="width:100%;height:100%;" fit="true">
    <div data-options="region:'center',split:true">
		<table id="demoData"></table>
	</div>
</div>  
<script>
var demoTable;
var toolbar = [{
	text:'批次上传',
    iconCls:'icon-add',
    handler:function(){
    	$.pmsDialog({
    		id:"plug_upload",  //固定ID
    		title:'上传控件',
    		isIf:'true',
    		params:{    //这4个必须填   可
        		"fileType":"all", //必填项  file  or   img or all
        		"fileExtensions":"", //可选值 可指定后缀 即上传类型
        		"oneSelect":'false',
        		"moduleId":"product",
        		"funcId":"batch",
        		"dataId":"<%=productCode%>"
        	},
    		method:'post',
    		href:'<c:url value="/views/jsp/upload/com_uploader_s.jsp"/>',
    		width:900,
    		height:464
    	});
    }
},{
	text:'商品资料上传',
    iconCls:'icon-add',
    handler:function(){
    	$.pmsDialog({
    		id:"plug_upload",  //固定ID
    		title:'上传控件',
    		isIf:'true',
    		params:{    //这4个必须填   可
        		"fileType":"all", //必填项  file  or   img or all
        		"fileExtensions":"", //可选值 可指定后缀 即上传类型
        		"oneSelect":'false',
        		"moduleId":"product",
        		"funcId":"paper",
        		"dataId":"<%=productCode%>"
        	},
    		method:'post',
    		href:'<c:url value="/views/jsp/upload/com_uploader_s.jsp"/>',
    		width:900,
    		height:464
    	});
    }
},{
	text:'数据删除',
    iconCls:'icon-remove',
    handler:function(){
    	removePicture();
    }
},{
	text:'保存',
    iconCls:'icon-save',
    handler:function(){
    	saveRowEdit();
    }
},{
	text:'【备注：批次+有效期为文件名可自动入库,如：20130405+2014-09-01.doc】'
    
}];

function initTable(){
	demoTable = $('#demoData').pDataGrid({
		toolbar:toolbar,
	    columns:[[
				  {field:'paperBatchId',checkbox:true},
	              {field:'view',title:'预览',align:'center',formatter:function(value,rows){
						return '<span style="padding:1px;"><a href="'+contentPath+rows.filePath+'"   target="_blank"><img src="'+contentPath+rows.fileIcon+'" width="60px;" height="60px;"  /></a></span>';
				  }},
			      {field:'paperBatchno',title:'批号',align:'center', editor: {type: 'text'}},
	              {field:'validDate',title:'有效期',align:'center', editor: {type: 'datebox'}},
	              {field:'fileName',title:'文件名',align:'center',formatter:function(value,rows){
	            	  return '<span style="padding:1px;"><a href="'+contentPath+rows.filePath+'"   target="_blank">'+rows.fileName+'</a></span>';
		            }},
	              {field:'type',title:'文件性质',align:'center'},
				  {field:'productCode',title:'产品编号',align:'center'},
	              {field:'productName',title:'产品名称',align:'center'},
	              {field:'remark',title:'备注',align:'center', editor: {type: 'text'}},
	              {field:'action',title:'操作',align:'center',formatter:function(value,row,index){
                      if (row.editing){
                          var s = '<a href="#" onclick="saverow('+index+')">保存</a> ';
                          var c = '<a href="#" onclick="cancelrow('+index+')">取消</a>';
                          return s+c;
                  		} else {
                          var e = '<a href="#" onclick="saverow('+index+')">保存</a> ';
                          var d = '<a href="#" onclick="cancelrow('+index+')">取消</a>';
                          return e+d;
                 	}      
                  }}
	        
	          ]],
	    url:'<c:url value="/product/searchEmBProductPaperBatch.do"/>',  
	    pagination:false,
	    singleSelect:false,
	    queryParams:{
			"productCode":"<%=productCode%>" 
		},
	    onBeforeEdit:function(index,row){
	         row.editing = true;
	        //$('#demoData').dataGrid('refreshRow', index);
	    },
	    onAfterEdit:function(index,row){
	        row.editing = false;
	        var param = {"paperBatchId":row.paperBatchId,
	        		     "paperBatchno":row.paperBatchno,
	        		     "remark":row.remark,
	        		     "validDate":row.validDate
	        		     };
	    	$.pmsAjax({
	    		url:'<c:url value="/product/updateEmBProductPaperBatch.do"/>',  
	    		data:param,
	    		success:function(data){
	    			if(data.code=='200'){
	    				demoTable.refresh();
	    			}else{ 
	    				alertError(data.result);
	    			}
	    		}
	    	});
	    },
	    onCancelEdit:function(index,row){
	        row.editing = true;
	        //$('#demoData').dataGrid('refreshRow', index);
	    },
  	    onDblClickCell: function(index,field,value){
  	    	$('#demoData').datagrid('beginEdit', index);
  	    }
	    
	}); 
}
function removePicture(){
	var row = $('#demoData').datagrid('getSelections');
	if(row.length==0){
		alertWarning("您未选择任何文件!");
		return;
	}
	var param = new Object();
	for(var i=0;i<row.length;i++){
		param['list['+i+'].paperBatchId']=row[i].paperBatchId;
	}
	$.pmsAjax({
		url:'<c:url value="/product/deleteEmBProductPaperBatch.do"/>',
		data:param,
		success:function(data){
			if(data.code=="200"){
				alertShow("删除成功");
				demoTable.refresh();
			}else{
				alertError(data.result);
			}
		}
	});
}
function getFiles(obj){
	if(obj.length>0){
		var param = new Object();
		for(var i=0;i<obj.length;i++){
			param['list['+i+'].productCode']="<%=productCode%>";
			param['list['+i+'].uploadId']=obj[i].id;
			param['list['+i+'].type']=obj[i].funcId;
			param['list['+i+'].fileName']=obj[i].fileName;
		}
		$.pmsAjax({
			url:'<c:url value="/product/insertEmBProductPaperBatch.do"/>',
			data:param,
			success:function(data){
				if(data.code=="200"){
					alertShow("新增成功");
					$("#plug_upload").dialog('close');
					demoTable.refresh();
				}else{
					alertError(data.result);
				}
			}
		});
	}
}

//保存行编辑
function saverow(index){
	$('#demoData').datagrid('endEdit',index);
}
function cancelrow(index){
	$('#demoData').datagrid('cancelEdit',index);
}
$(function(){
	initTable();



});


</script>
